 async function flushBottomFunc() {
     const data = [
         ['我的收藏', 'heart', 'shoucang.html'],
         ['活动广场', 'huodong', ''],
         ['关于猫协', 'guanyu', ''],
         ['关于捐赠', 'juanzeng', ''],
         [],
         ['反馈', 'fankui', ''],
         ['客服', 'kefu', ''],
         [],
         ((await login_check()) ? ['登出', 'log-out', "javascript:logout()"] : ['登录', 'log-in', '#nav_logIR-login']),
         ((await login_check()) ? null : ['注册', 'register', '#nav_logIR-register']),
         (Object.keys(await getUserInfo('permission')).length ? ['管理', 'management', 'admin.html'] : null)
     ];
     const emp = '<span></span>';
     const c = 'class="module l-module iconTextLine"';
     const tar = $("#bottomFunc");
     tar.html('');
     for (i in data) {
         if (data[i] == null) continue;
         let L = data[i][2],
             I = data[i][1],
             T = data[i][0];
         if (!(L || I || T)) {
             tar.append(`<span style="background-color:#FFF;height:10vw;"${c}></span>`);
             continue;
         }
         let ele = `<a${L?` href="${L}" `:' '}${c}>`;
         ele += (I ? `<svg class="icon" aria-hidden="true"><use xlink:href="#icon-${I}"></use></svg>` : emp);
         ele += (T ? `<span>${T}</span>` : emp);
         ele += (L ? '<span>&gt;</span>' : emp);
         tar.append(ele);
     }
 }

 //加密算法
 function encryption(pwd) {
     return CryptoJS.enc.Base64.stringify(CryptoJS.SHA256(pwd));
 }

 function login() {
     let name = $('#l_username').val();
     let pwd = $('#l_password').val();

     let time = new Date().getTime();
     pwd = encryption(encryption(pwd) + time);
     $.post("/user/auth", { "username": name, "password": pwd, "time": time },
         (result, statue) => {
             if (result.success) {
                 localStorage.setItem("token", result.message);
                 alert("登录成功");
                 location.hash = "";
                 location.reload();
             } else {
                 $("#logIR-login-result").html(result.message).show();
                 // alert(result.message);
                 console.log(result.message);
             }
         }
     );
 }

 function logout() {
     if (!confirm("确定要退出登录嘛?")) return;
     localStorage.setItem("token", "");
     $.get("/user/logout", r => {
         location.hash = "";
         location.reload();
     });
 }

 function register_idCode() {
     let invite = $('#r_invite').val();
     let email = $('#r_email').val();
     $.get(`/user/register/verify-email?email=${email}&invite=${invite}`, r => {
         $("#logIR-register-result").html(`获取验证码: ${r.success?`成功, ${r.evt/1000/60} 分钟后失效`:`失败, ${r.err}`}`).show();
     });
 }

 function register() {
     let username = $('#r_username').val();
     let password = $('#r_password').val();
     let password2 = $('#r_password2').val();
     let email = $('#r_email').val();
     let idCode = $('#r_idCode').val();
     let invite = $('#r_invite').val();

     let err = x => $("#logIR-register-result").html(x).show();

     if (password != password2) return err('两次密码不一致');
     if (!(username && password && email && idCode && r_invite)) return err('请填写全信息');
     // err('成功');

     $.post("/user/register/auth", {
             username: username,
             password: password,
             email: email,
             idCode: idCode,
             invite: invite,
         },
         (result, statue) => {
             if (result.success) {
                 alert("注册成功");
             } else {
                 $("#logIR-login-result").html(result.err).show();
             }
         }
     );

 }

 function onScrollBottom() {
     login_check(s => {
         if (!s) return;
         let id = $(".main-content.active").attr('id');
         // console.log('onScrollBottom - id', id);
         let uid = userInfo.info.uid;
         if (id === 'threads')
             getThread(uid);
         else if (id === 'fansfollow')
             getFansFollow(uid);
     });
 };
 var getFansFollow = (() => {
     let page_fans = 0,
         page_fols = 0;
     let page_fansEnd = false,
         page_folsEnd = false;
     const __errorHandleFans = (obj, err, msg) => {
         $("#content_fansfollow-fans").html(`<span><h1>${err}</h1>${msg}</span>`);
         page_fans--;
     };
     const __errorHandleFols = (obj, err, msg) => {
         $("#content_fansfollow-follow").html(`<span><h1>${err}</h1>${msg}</span>`);
         page_fols--;
     };
     const ele = (id, f, ft) => {
         $.get('/user/getUserInfo/' + id, r => {
             if (r.success) {
                 r = r.result;
                 $(`.user-id-${id} .name`).html(r.nick || r.name);
                 $(`.user-id-${id} .autograph`).html(r.name);
             } else {
                 $(`.user-id-${id} .name`).html(r.err);
             }
         });
         $.get('/img/user?uid=' + id, (r, s, x) => {
             let type = (x.getResponseHeader('Content-Type') || "").trim().toLowerCase(),
                 t = $(`.user-id-${id} .sketch`);
             if (type === 'text/plain') t.replaceWith(`<svg class="icon sketch"aria-hidden="true"><use xlink:href="#icon-${r}"></use></svg>`);
             else t.attr('src', r);
         });
         return `<span class="module l-module infoLine user-id-${id}"><img class="sketch" src="https://placehold.it/256x256&text=USER"><svg class="icon mark-icon" aria-hidden="true"><use xlink:href="#icon-paw"></use></svg><span class="name">-</span><span class="autograph">-</span><span  class="follow${f?' followed':''}"onclick="callFollowBtn(this,${id},undefined,'${ft}')">${f?ft:'关注'}</span></span>`;
     }
     return x => {
         if ($("#nav_fansfollow-fans").hasClass("active")) {
             if (page_fansEnd) return;
             if (page_fans == 0) $("#content_fansfollow-fans").html('');
             $.ajax({
                 url: `/user/fans?page=${page_fans++}`,
                 error: __errorHandleFans,
                 success: (responseJSON) => {
                     if (!responseJSON.success) return __errorHandleFans('fansListError', responseJSON.err);
                     let r = responseJSON.result;
                     page_fansEnd |= !(r.length > 0);
                     for (let i in r) $("#content_fansfollow-fans").append(ele(r[i].tid, r[i].also, '互相关注'));
                 }
             });
         } else {
             if (page_folsEnd) return;
             if (page_fols == 0) $("#content_fansfollow-follow").html('');
             $.ajax({
                 url: `/user/follows?page=${page_fols++}`,
                 error: __errorHandleFols,
                 success: (responseJSON) => {
                     if (!responseJSON.success) return __errorHandleFols('fansListError', responseJSON.err);
                     let r = responseJSON.result;
                     page_folsEnd |= !(r.length > 0);
                     for (let i in r) $("#content_fansfollow-follow").append(ele(r[i].fid, true, '已关注'));
                 }
             });
         }
     }
 })();
 var getThread = (() => {
     let page_me = 0,
         page_star = 0;
     let page_meEnd = false,
         page_starEnd = false;
     const __errorHandleMe = (obj, err, msg) => {
         $("#content_threads").html(`<span><h1>${err}</h1>${msg}</span>`);
         page_me--;
     };
     const __errorHandleStar = (obj, err, msg) => {
         $("#content_threads_star").html(`<span><h1>${err}</h1>${msg}</span>`);
         page_star--;
     };
     return async id => {
         if (!id) id = (await login_check()) && userInfo.info.uid;
         if (!id) return;
         if ($("#nav_threads").hasClass("active")) {
             if (page_meEnd) return;
             if (page_me == 0) $("#content_threads").html('');
             $.ajax({
                 url: `/threads/list?type=usr&page=${page_me++}&uid=${id}`,
                 error: __errorHandleMe,
                 success: (responseJSON) => {
                     if (!responseJSON.success) return __errorHandleMe('meThreadListError', responseJSON.err);
                     let r = responseJSON.result;
                     page_meEnd |= !(r.length > 0);
                     for (let i in r) {
                         i = toRealName(r[i]);
                         let ele = `<a href="/public/threads.html?tid=${i.id}" class="module m-module thread"><img class="sketch"src="/img/thread/${i.pid}" />${/**头像, 直接复制*/$('<p>').append($('#sketch').clone().addClass('userHead').attr('id',null)).remove().html()}<span class="user">${userInfo.info.nick||userInfo.info.name}</span><span class="description">${i.title}</span><span class="love"><svg class="icon"aria-hidden="true"><use xlink:href="#icon-heart"></use></svg><span class="love-amount">${i.like}</span></span></a>`
                         $("#content_threads").append(ele);
                     }
                 }
             });
         } else {
             //TODO 收藏的帖子
         }
     }
 })();

 function getUserSketch(R) {
     let S = r => $("#sketch").replaceWith(`<svg class="icon"id="sketch"aria-hidden="true"><use xlink:href="#icon-${r}"></use></svg>`);
     if (R) return S(R), $("#sketch").click(sketchClick);
     $.get('/img/user', (r, s, x) => {
         let type = (x.getResponseHeader('Content-Type') || "").trim().toLowerCase();
         if (type === 'text/plain') S(r);
         else $("#sketch").attr('src', r);
         $("#sketch").click(sketchClick)
     });
 }
 $(() => {
     flushBottomFunc();
     login_check(success => {
         let info = userInfo.info || {};
         $("#userName").html(info.name || "未登录");
         console.log($("#sketch").error);
         if (success) {
             $.get('/user/fans-count', r => $("#fans").html(r.success ? r.count : r.err));
             $.get('/user/follows-count', r => $("#follows").html(r.success ? r.count : r.err));
             $.get('/threads/my-count', r => $("#dynamics").html(r.success ? r.count : r.err));
             getUserSketch();
         } else $("#sketch").click(() => location.hash = 'nav_logIR-login');
     });

 });

 function sketchClick() {
     console.log(1);
     $("<div class='inquiryBoxBackground'>").appendTo($("body")).click(e => $(".inquiryBoxBackground,.inquiryBox").remove());
     let box = $("<div class='inquiryBox'>").appendTo($("body"));
     const card = (i, t) => $("<div>").appendTo($("<div>").appendTo(box)).html(`${icon(i)}<span>${t}</span>`);
     card('xuanze', '选择头像').click(() => {
         box.addClass('selectBox').html('');
         const handler = e => (e = (e = $(e.target)).hasClass('icon') ? e : e.parents('svg.icon'), $.get("/user/set-sketchIcon", { icon: (e = e.attr('data-icon')) }, r => r.success ? getUserSketch(e) : alert(r.err)));
         [...$("symbol")].forEach(e => {
             $(icon(e.id)).attr('data-icon', e.id.substring('icon-'.length)).click(handler).appendTo(box);
         });
     });
     card('wenhao', '默认头像').click(() => $.get("/user/set-sketchIcon", r => r.success ? getUserSketch() : alert(r.err)));
 }

 nav_useSubChange = true;